﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using Telerik.Windows.Controls;
using ZL.Push.Web;


namespace ZL.Push
{
    public partial class ShareToFriendControlBase : BaseReadonlyControl<tbl_friend>
    {
    }

    public partial class ShareToFriendControl : ShareToFriendControlBase
    {
        protected tbl_metadata _metadata = null;


        public ShareToFriendControl()
        {
            InitializeComponent();
            base.InitializeComponent("tbl_friend");
        }


        public void search(tbl_metadata metadata)
        {
            _metadata = metadata;

            _vm.lambda = _create_lamda();
            _vm.OnSearch();
        }


        protected override Expression<Func<tbl_friend, bool>> _create_lamda()
        {
            if (_metadata == null || string.IsNullOrEmpty(_metadata.share_friend_ids))
            {
                return App.s_create_no_result_lamda(typeof(tbl_friend)) as Expression<Func<tbl_friend, bool>>;
            }

            List<SearchParameter> lst = new List<SearchParameter>();

            foreach (string s in _metadata.share_friend_ids.Split('-'))
            {
                if (string.IsNullOrEmpty(s))
                {
                    continue;
                }

                lst.Add(new SearchParameter()
                {
                    Relation = EnumRelation.Or,
                    PropertyName = "friend_id",
                    Operator = EnumOperator.Equal,
                    PropertyValue = Convert.ToInt32(s),
                    TypeOfValue = typeof(int),
                });
            }

            return App.s_create_lamda_with_user(typeof(tbl_friend), null, ref lst)
                        as Expression<Func<tbl_friend, bool>>;
        }


        #region datagrid相关函数
        protected override void _datagrid_LoadingRow(object sender, DataGridRowEventArgs e)
        {
            StackPanel sp = _datagrid.Columns[0].GetCellContent(e.Row) as StackPanel;
            TextBlock cell = sp.Children.ElementAt(0) as TextBlock;
            if (_datapager != null)
            {
                cell.Text = (_datapager.PageIndex * _datapager.PageSize + e.Row.GetIndex() + 1).ToString() + " ";
            }
            else
            {
                cell.Text = (e.Row.GetIndex() + 1).ToString() + " ";
            }
        }

        protected override void _datagrid_OnDoubleClicked(object sender, EventArgs e)
        {
            if (_datagrid.SelectedItem == null)
            {
                return;
            }

            FriendWindow win = new FriendWindow(AddOrEditType.eNone);
            win.DataContext = _datagrid.SelectedItem as tbl_friend;
            win.Show();
        }
        #endregion


        #region menu相关函数
        private void _menu_datagrid_Opened(object sender, RoutedEventArgs e)
        {
            RadContextMenu menu = sender as RadContextMenu;

            (menu.Items.ElementAt(1) as RadMenuItem).IsEnabled = _datagrid.SelectedItem != null;
            (menu.Items.ElementAt(2) as RadMenuItem).IsEnabled = _vm.source.Count > 0;
        }

        private void _menu_datagrid_ItemClick(object sender, Telerik.Windows.RadRoutedEventArgs e)
        {
            Telerik.Windows.Controls.RadMenuItem clickedItem = e.OriginalSource as Telerik.Windows.Controls.RadMenuItem;
            switch (clickedItem.Tag.ToString())
            {
            case "0":
                _add_share();
                break;
            case "1":
                _remove_share();
                break;
            case "2":
                _remove_all_share();
                break;
            default:
                break;
            }
        }
        #endregion


        #region 操作函数
        protected void _add_share()
        {
            if (_metadata == null)
            {
                return;
            }

            List<tbl_metadata> lst = new List<tbl_metadata>();
            lst.Add(_metadata);
            MetadataFormFunction.s_share_to_friend(lst, (ok) =>
            {
                if (ok)
                {
                    if (!string.IsNullOrEmpty(_metadata.share_friend_ids))
                    {
                        search(_metadata); // 刷新
                    }
                    else
                    {
                        clear(); // 刷新
                    }
                }
            });
        }

        protected void _remove_share()
        {
            if (_metadata == null)
            {
                return;
            }
            if (_datagrid.SelectedItems.Count > 1)
            {
                if (MessageBox.Show("是否取消当前文献与所选 " + _datagrid.SelectedItems.Count + " 个好友的共享？", "警告", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
                {
                    return;
                }
            }
            else
            {
                tbl_friend tbl = _datagrid.SelectedItem as tbl_friend;
                if (MessageBox.Show("是否取消当前文献与所选好友 " + tbl.nick_name + " 的共享？", "警告", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
                {
                    return;
                }
            }

            List<tbl_metadata> lst = new List<tbl_metadata>();
            lst.Add(_metadata);
            MetadataFormFunction.s_unshare_to_friend(lst, _datagrid.SelectedItems, (ok) =>
            {
                if (ok)
                {
                    if (!string.IsNullOrEmpty(_metadata.share_friend_ids))
                    {
                        search(_metadata); // 刷新
                    }
                    else
                    {
                        clear(); // 刷新
                    }
                }
            });
        }

        protected void _remove_all_share()
        {
            if (_metadata == null)
            {
                return;
            }
            if (MessageBox.Show("是否取消当前文献所有给好友的共享？", "警告", MessageBoxButton.OKCancel) != MessageBoxResult.OK)
            {
                return;
            }

            List<tbl_metadata> lst = new List<tbl_metadata>();
            lst.Add(_metadata);
            MetadataFormFunction.s_unshare_to_friend(lst, null, (ok) =>
            {
                if (ok)
                {
                    if (!string.IsNullOrEmpty(_metadata.share_friend_ids))
                    {
                        search(_metadata); // 刷新
                    }
                    else
                    {
                        clear(); // 刷新
                    }
                }
            });
        }
        #endregion
    }
}